package aom.ju.ss.view.user.set

import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import aom.ju.ss.R
import aom.ju.ss.databinding.MyQrCodeBinding
import aom.ju.ss.mode.UserInfo
import aom.ju.ss.utils.FileUtils
import aom.ju.ss.utils.LogUtils
import aom.ju.ss.utils.show
import aom.ju.ss.utils.zxing.activity.CodeUtils
import aom.ju.ss.view.base.BaseActivity
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition


class MyQrCodeActivity : BaseActivity() {
    private lateinit var binding: MyQrCodeBinding
    override fun init() {
        binding = viewBinding as MyQrCodeBinding

        binding.info = UserInfo.userInfo
        UserInfo.userInfo?.sex?.let {
            when (it) {
                1 -> {
                    binding.sex.setBackgroundResource(R.drawable.ic_nan)
                }
                2 -> {
                    binding.sex.setBackgroundResource(R.drawable.ic_nv)
                }
            }
        }

        Glide.with(this)
            .asBitmap().load(UserInfo.userInfo?.headimgurl)
            .into(object : CustomTarget<Bitmap>() {
                override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                    LogUtils.e(UserInfo.userInfo?.member_id)
                    val mBitmap = CodeUtils.createImage(
                        UserInfo.userInfo?.member_id, 400, 400, resource
                    )

                    binding.img.setImageBitmap(mBitmap)

                    if (!FileUtils.newFileAN("qr.png").exists()){
                        FileUtils.saveBitmapFile(resource, FileUtils.newFileAN("qr.png").absolutePath)
                    }

                }

                override fun onLoadCleared(placeholder: Drawable?) {
                }

            })

        binding.titleView4.r_title.setOnClickListener {
            "保存成功".show

        }

    }

    override val lay: Int = R.layout.my_qr_code
}